﻿// **********************************************************************
//
// Author: KingSley
// Email:3268374537@qq.com
// Created:	2018/8/1   10:00
// Introduce: 摄像机的控制方式，第一人称鼠标位置控制摄像机
//
// **********************************************************************
using UnityEngine;

namespace KingSley
{
    [AddComponentMenu("KingARPG/摄像机/第一人称鼠标控制摄像机")]
    /// <summary>
    /// 通过鼠标位置与滚轮控制摄像机方向
    /// </summary>
    public class CameraWithTheMouse : MonoSingleton<CameraWithTheMouse>
    {
        /// <summary>
        /// //观察目标
        /// </summary>
        public Transform Target;
        /// <summary>
        /// 观察距离
        /// </summary>
        public float Distance = 10.0F;
        /// <summary>
        /// X轴和Y轴的旋转速度
        /// </summary>
        private float SpeedX = 120;
        private float SpeedY = 120;
        /// <summary>
        /// 角度限制
        /// </summary>
        private float MinLimitY = 5;
        private float MaxLimitY = 360;
        /// <summary>
        /// 旋转角度
        /// </summary>
        private float mX = 0.0F;
        private float mY = 0.0F;
        /// <summary>
        /// 鼠标缩放距离最值
        /// </summary>
        private float MaxDistance = 15.0f;
        private float MinDistance = 5.0f;
        /// <summary>
        /// 鼠标缩放速率
        /// </summary>
        private float ZoomSpeed = 10f;
        /// <summary>
        /// 是否启用差值
        /// </summary>
        public bool isNeedDamping = true;
        /// <summary>
        /// 缩放速度
        /// </summary>
        public float Damping = 20f;
        /// <summary>
        /// 经计算后，旋转到的角度，以便平滑旋转
        /// </summary>
        private Quaternion CRotation;
        private Vector3 CPosition;
        /// <summary>
        /// 位置临时存储变量
        /// </summary>
        private Transform Temp_transform;
        public Vector3 LookAtPoint;

        private bool closer = false;

        void Awake()
        {
            Temp_transform = transform;
        }

        void Start()
        {
            // 初始化旋转角度
            mX = transform.eulerAngles.x;
            mY = transform.eulerAngles.y;
        }
        void LateUpdate()
        {
            if (Target != null)
            {
                // 获取鼠标输入
                mX += Input.GetAxis("Mouse X") * SpeedX * 0.02F;
                mY -= Input.GetAxis("Mouse Y") * SpeedY * 0.02F;
                // 计算旋转
                CRotation = Quaternion.Euler(mY, mX, 0);

                // 角度限制
                mY = LimitAngle(mY, MinLimitY, MaxLimitY);


                if (isNeedDamping)
                {
                    transform.rotation = Quaternion.Lerp(transform.rotation, CRotation, Time.deltaTime * Damping);
                }
                else
                {
                    transform.rotation = CRotation;
                }

                // 鼠标滚轮缩放
                Distance -= Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
                Distance = Mathf.Clamp(Distance, MinDistance, MaxDistance);

                // 重新计算位置
                CPosition = CRotation * new Vector3(0.0F, 0.0F, -Distance) + Target.transform.position;

                // 设置相机的角度和位置
                if (isNeedDamping)
                {
                    transform.position = Vector3.Lerp(transform.position, CPosition, Time.deltaTime * Damping);
                    var ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
                    LookAtPoint = ray.GetPoint(20.0f);
                }
                else
                {
                    transform.position = CPosition;
                }
            }
        }
        private float LimitAngle(float angle, float min, float max)
        {
            if (angle < -360)
            {
                angle += 360;
            }
            if (angle > 360)
            {
                angle -= 360;
            }
            return Mathf.Clamp(angle, min, max);
        }
    }
}
